﻿namespace VirtualTemplates
{
    using System.Diagnostics.Contracts;
    using System.IO;
    using System.Web.Hosting;

    /// <summary>
    /// A virtual file that maps to a physical file.
    /// </summary>
    internal class MappedVirtualFile : VirtualFile
    {
        /// <summary>
        /// The file info of the physical file.
        /// </summary>
        private readonly FileInfo fileInfo;

        /// <summary>
        /// Initializes a new instance of the <see cref="MappedVirtualFile"/> class.
        /// </summary>
        /// <param name="fileInfo">The file info of the physical file.</param>
        /// <param name="virtualPath">The virtual path.</param>
        public MappedVirtualFile(FileInfo fileInfo, string virtualPath) : base(virtualPath)
        {
            Contract.Requires(fileInfo != null);
            
            this.fileInfo = fileInfo;
        }

        /// <summary>
        /// Rreturns a read-only stream to the physical file.
        /// </summary>
        /// <returns>
        /// A read-only stream to the physical file.
        /// </returns>
        public override Stream Open()
        {
            return File.Open(this.fileInfo.FullName, FileMode.Open);
        }
    }
}
